package com.longqinuo.mycarservice.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.longqinuo.mycarservice.entity.KeepServer;
import com.longqinuo.mycarservice.manager.DatabaseOpenHelper;


/**
 * dao层实现类 使用SQLiteDatabase对象增删改查
 * Created by zxw on 2016/5/20.
 */
public class ServerDaoImpl implements IServerDao {
    private SQLiteDatabase database;
    public ServerDaoImpl(Context context) {
        DatabaseOpenHelper helper = new DatabaseOpenHelper(context);
        database = helper.getWritableDatabase();
    }

    @Override
    public long add(KeepServer k) {
        long result = 0;
        if (!isExists(k.getId())) {
            ContentValues values = new ContentValues();
            values.put("server_Id",k.getId());
            values.put("name",k.getName());
            values.put("image",k.getImage());
            values.put("count",k.getCount());
            values.put("location",k.getCom_location());
            values.put("price",k.getPrice());
            values.put("originaPrice",k.getOriginaPrice());
            result = database.insert(DatabaseOpenHelper.SERVER_TABLE,null,values);
        }
        return result;
    }

    @Override
    public boolean isExists(int serverId) {
        Cursor cursor = database.query(DatabaseOpenHelper.SERVER_TABLE,null,"server_Id = ?",new String[]{serverId+""},null,null,null);
        if (cursor!=null&&cursor.getCount()>0) {

            return true;
        } else {
            return false;
        }
    }

    @Override
    public Cursor findServer(KeepServer k) {
        Cursor cursor = database.query(DatabaseOpenHelper.SERVER_TABLE,null,"server_Id = ?",new String[]{k.getId()+""},null,null,null);
        return cursor;
    }

    @Override
    public Cursor findAllServers() {
        Cursor cursor = database.query(DatabaseOpenHelper.SERVER_TABLE,null,null,null,null,null,null);
        return cursor;
    }

    @Override
    public int delete(KeepServer k) {
        int i = database.delete(DatabaseOpenHelper.SERVER_TABLE,"server_Id = ?",new String[]{k.getId()+""});
        return i;
    }


}
